import ServerCenter from '../../Server/ServerCenter';
import GameUtil from '../../Fantasy/Util/GameUtil';
import LevelInfoCard from '../PrefabJs/LevelInfoCard';
import SoundUtil from '../../Fantasy/Util/SoundUtil';
// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class RushLayer extends cc.Component {

    @property(cc.Label)
    stamina: cc.Label = null;
    @property(cc.Label)
    exp: cc.Label = null;
    @property(cc.PageView)
    PageContent : cc.PageView = null;

    @property(cc.Prefab)
    LevelInfoCardPrefab : cc.Prefab = null;

    @property(cc.Prefab)
    setting : cc.Prefab = null;

    // LIFE-CYCLE CALLBACKS:



    start () {
        this.exp.string = ServerCenter.getUserExp() + '';
        this.stamina.string = ServerCenter.getUserStamina() + '';
    }
    onEnable(){

        this.createLevelCard();
    }

    createLevelCard(){

        this.PageContent.removeAllPages();
        let maxLevel = ServerCenter.getMaxLevel(GameUtil.openBookId);
        let unLockLevel = ServerCenter.getCurrLevel(GameUtil.openBookId);
        for(let i=1;i<=maxLevel;i++){
            let node = cc.instantiate(this.LevelInfoCardPrefab);
            let script = node.getComponent(LevelInfoCard);
            script.setLevelCard(GameUtil.openBookId,i,i>unLockLevel);
            this.PageContent.addPage(node);
        }
        setTimeout(()=>{

            this.PageContent.scrollToPage(unLockLevel-1,1);
        },100)
    }
    // update (dt) {}
    btnCallback(target,data){

        SoundUtil.playEffect("music/click");
        if (data == 'close'){
            this.node.active = false;
        }else if(data == "setting"){
            let node = cc.instantiate(this.setting);
            node.parent = this.node;
        }
    }
}
